package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.RFavoriteSource;
import com.jilefojing.beans.pojos.User;
import com.jilefojing.beans.query.RFavoriteSourceQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IRFavoriteSourceDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IRFavoriteSourceMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月23日 02时21分15秒
 */
@Repository
public class RFavoriteSourceDao extends BaseDao<RFavoriteSource> implements IRFavoriteSourceDao {
	@Autowired
	private IRFavoriteSourceMapper rFavoriteSourceMapper;

	@Override
	public RFavoriteSource getByItemForUser(ItemTypeEnumForSource itemTyp, long itemId, User user) {
		List<RFavoriteSource> rFavoriteSources = this.getByItemsForUser(itemTyp, Arrays.asList(new Long[] { itemId }), user);
		if (!Detect.notEmpty(rFavoriteSources)) {
			return null;
		}
		return rFavoriteSources.get(0);
	}

	@Override
	public List<RFavoriteSource> getByItemsForUser(ItemTypeEnumForSource itemTyp, List<Long> itemIds, User user) {
		RFavoriteSourceQuery favoriteSourceQuery = new RFavoriteSourceQuery();
		favoriteSourceQuery.setItemTypes(Arrays.asList(new ItemTypeEnumForSource[] { itemTyp }));
		favoriteSourceQuery.setItemIds(itemIds);
		favoriteSourceQuery.setUserIds(Arrays.asList(new Long[] { user.getId() }));
		return this.getPagination(favoriteSourceQuery, new Pagination<RFavoriteSource>(1, Integer.MAX_VALUE, false)).getItems();
	}

	@Override
	public Pagination<RFavoriteSource> getPagination(RFavoriteSourceQuery rFavoriteSourceQuery, Pagination<RFavoriteSource> pagination) {
		Example example = new Example(RFavoriteSource.class);
		Criteria criteria = example.createCriteria();

		if (Detect.notEmpty(rFavoriteSourceQuery.getIds())) {
			criteria.andIn(RFavoriteSource.FIELD_ID, rFavoriteSourceQuery.getIds());
		}

		if (Detect.notEmpty(rFavoriteSourceQuery.getItemTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _itemType : rFavoriteSourceQuery.getItemTypes()) {
				itemTypeCodes.add(_itemType.getCode());
			}
			criteria.andIn(RFavoriteSource.FIELD_ITEMTYPE, itemTypeCodes);
		}

		if (Detect.notEmpty(rFavoriteSourceQuery.getItemIds())) {
			criteria.andIn(RFavoriteSource.FIELD_ITEMID, rFavoriteSourceQuery.getItemIds());
		}

		if (Detect.notEmpty(rFavoriteSourceQuery.getFavoriteIds())) {
			criteria.andIn(RFavoriteSource.FIELD_FAVORITEID, rFavoriteSourceQuery.getFavoriteIds());
		}

		if (Detect.notEmpty(rFavoriteSourceQuery.getUserIds())) {
			criteria.andIn(RFavoriteSource.FIELD_USERID, rFavoriteSourceQuery.getUserIds());
		}

		if (rFavoriteSourceQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(RFavoriteSource.FIELD_CREATETIME, rFavoriteSourceQuery.getEqualCreateTime());
		}

		if (rFavoriteSourceQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(RFavoriteSource.FIELD_CREATETIME, rFavoriteSourceQuery.getEqualAndBeforCreateTime());
		}

		if (rFavoriteSourceQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(RFavoriteSource.FIELD_CREATETIME, rFavoriteSourceQuery.getEqualAndAfterCreateTime());
		}

		if (rFavoriteSourceQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(RFavoriteSource.FIELD_CREATETIME, rFavoriteSourceQuery.getBeforCreateTime());
		}

		if (rFavoriteSourceQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(RFavoriteSource.FIELD_CREATETIME, rFavoriteSourceQuery.getAfterCreateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return super.getPagination(pagination, example);
	}
}
